package com.zsc.wxm.ysh.anjo.es.controller;

import com.zsc.wxm.ysh.anjo.es.service.UploadService;
import com.zsc.wxm.ysh.anjo.es.task.PullDataToEsByCrawlerTask;
import com.zsc.wxm.ysh.anjo.result.Result;
import com.zsc.wxm.ysh.anjo.service.base.model.es.vo.EsDataVO;
import com.zsc.wxm.ysh.anjo.utils.ExceptionUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 描述：
 *
 * @author playmaker
 * @since 2021/2/14 9:52
 */
@RestController
@RequestMapping("/es")
@Slf4j
public class UploadController {

    @Resource
    private UploadService uploadService;

    @Resource
    private PullDataToEsByCrawlerTask task;

    @PostMapping("/upload")
    @ApiOperation("上传爬虫爬取的图片")
    public Result upload() {
        uploadService.uploadHouseImage();
        return Result.ok("上传成功");
    }

    @PostMapping("/upload-es-data")
    @ApiOperation("上传房源数据到ES中")
    public Result uploadEsData(@RequestBody List<EsDataVO> list) {
        try {
            boolean flag = uploadService.uploadHouseResourcesByCrawler(list);
            if (!flag) {
                log.error("上传房源数据到ES中失败");
            }
        } catch (IOException e) {
            log.error(ExceptionUtil.getMessage(e));
        }
        return Result.ok("导入成功");
    }

    @PostMapping("/crawler")
    @ApiOperation("执行爬虫定时任务")
    public Result uploadByCrawler() {
        task.execute();
        return Result.ok("success");
    }
}
